﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace T_Material.Infrastructure
{
    public class BaseRepository<T>:IRepository<T> where T : class, new()
    {
        protected readonly EFCoreDbcontext dbcontext;
        public BaseRepository(EFCoreDbcontext dbcontext)
        {
            this.dbcontext = dbcontext;
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public async Task<int>Addsync(T t)
        {
            await dbcontext.AddAsync(t);
            return await dbcontext.SaveChangesAsync();
        }

        /// <summary>
        /// 显示
        /// </summary>
        /// <returns></returns>
        public async Task<List<T>>GetAllAsync()
        {
            return await dbcontext.Set<T>().ToListAsync();
        }

        /// <summary>
        /// 条件显示
        /// </summary>
        /// <param name="expre"></param>
        /// <returns></returns>
        public async Task<List<T>>GetAsync(Expression<Func<T, bool>> expre)
        {
            return  await dbcontext.Set<T>().Where(expre).ToListAsync();
        }

        /// <summary>
        /// 查找id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<T>GetModelAs(int id)
        {
            return await dbcontext.Set<T>().FindAsync(id);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public async Task<int> Update(T t)
        {
            dbcontext.Update(t);
            return await dbcontext.SaveChangesAsync();
        }

        public async Task<int> Delete(int id)
        {
            DbSet<T> dbset = dbcontext.Set<T>();
            var res = dbset.Find(id);
            dbset.Remove(res);
            return await dbcontext.SaveChangesAsync();
        }

        public async Task<int> DeleteInfo(Expression<Func<T, bool>> info)
        {
            DbSet<T> dbset = dbcontext.Set<T>();
            var res = dbset.Where(info);
            dbset.RemoveRange(res);
            return await dbcontext.SaveChangesAsync();
        }

        public async Task<T> GetInfo(Expression<Func<T, bool>> info)
        {
            DbSet<T> dbset = dbcontext.Set<T>();
            return await dbset.FirstOrDefaultAsync(info);
        }

        public async Task<T> Get(int id)
        {
            DbSet<T> dbset = dbcontext.Set<T>();
            return await dbset.FindAsync(id);
        }

        public async Task<List<T>> GetList()
        {
            return await dbcontext.Set<T>().AsNoTracking().ToListAsync();
        }

        public async Task<int> UpdateAsyncRange(List<T> info)
        {

            dbcontext.UpdateRange(info);
            return await dbcontext.SaveChangesAsync();
        }

    }
}
